package ch.zbw.sysventory.server.control.validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import ch.zbw.sysventory.server.model.User;

@Component
public class UserValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.equals(clazz);
	}

	@Override
	public void validate(Object object, Errors errors) {
		if (object instanceof User) {
			User user = (User) object;
			if (user.getLastName() == null || "".equals(user.getLastName())) {
				errors.rejectValue("lastName", "emptyField.user.lastName");
			}
			if (user.getFirstName() == null || "".equals(user.getFirstName())) {
				errors.rejectValue("firstName", "emptyField.user.firstName");
			}
			if (user.getLoginName() == null || "".equals(user.getLoginName())) {
				errors.rejectValue("loginName", "emptyField.user.loginName");
			}
		}
	}
}
